package coop.bancocredicoop.proyectos.gd.reports;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Projections;

import coop.bancocredicoop.proyectos.gd.domain.DocumentType;

public class DocumentTypesReport {
	
	public QueryResult getAll(Session session, Integer offset, Integer max) {
		
		Integer totalItems =  ((Number) session.createCriteria(DocumentType.class).setProjection(Projections.rowCount()).uniqueResult()).intValue();
	    
	    Query q = session.createQuery("from DocumentType ORDER BY name")
	    		  .setMaxResults(max)
	    		  .setFirstResult(offset);
	    
	    List<DocumentType> documentTypes = (List<DocumentType>) q.list();
	    List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
	    
	    for(DocumentType documentType: documentTypes) {
	    	Map<String, Object> item = new HashMap<String, Object>();
	    	item.put("id", documentType.getId());
	    	item.put("shortName", documentType.getShortName());
	    	item.put("name", documentType.getName());
	    	item.put("purgeable", documentType.getPurgeable());
	    	item.put("hasExpiration", documentType.getHasExpiration());
	    	item.put("daysInAdvanceForNotice", documentType.getDaysInAdvanceForNotice());
	    	item.put("defaultLastingMonths", documentType.getDefaultLastingMonths());
	    	item.put("uploadNotificationEmailAddress", documentType.getUploadNotificationEmailAddress());
	    	result.add(item);
	    }
	    
	    return new QueryResult(result, totalItems, offset);
	    
	}
	
}
